#!/usr/bin/python
# coding=utf-8
"""
Load some testdata
"""
__revision__ = "$Id: basedata.py 46 2007-03-12 09:42:24Z jens.persson $"

import datetime

from worde.model import Configuration, Post, User, Group, Comment
from worde.model import PluginConfiguration, hub
import worde.model
from worde import plugins
import sqlobject

import pkg_resources
pkg_resources.require("TurboGears")

from turbogears import update_config
import cherrypy
cherrypy.lowercase_api = True

update_config(configfile="dev.cfg", modulename="worde.config")

def clear_all():
    """
    Clean out the database. Eg. drop it and then recreate it
    Uses tg-admin
    """
    from os import system
    system("tg-admin sql drop")
    system("tg-admin sql create --class=Post")
    system("tg-admin sql create")

def setup_tables():
    """
    Clean out the database, using the SQLobjects direct
    """
    module = worde.model
    names = worde.model.__dict__
    classes = [names[c] for c in names
               if isinstance(names[c], type)
                  and issubclass(names[c], sqlobject.SQLObject)
                  and names[c].__module__ == module.__name__]
    for c in classes:
        try:
            c.dropTable()
        except:
            pass
        c.createTable()

def load_startdata():
    """
    Load a collection of data for testing purposes
    """
    Configuration(name="blog title", type="string", value="The Ankh-Morpork Times")
    Configuration(name="copyright info", type="string",
                  value="Copyright the Year of The Fruitbat, William de Worde")
    Configuration(name="posts on frontpage", type="integer", value=10)
    Configuration(name="admin locale", type="string", value="Auto")
    Configuration(name="global locale", type="string", value="en")
    Configuration(name="Comment moderation blacklist", type="string", value="rolex")
    Configuration(name="Comment block blacklist", type="string", value="xxx")

    u = User(user_name="jens", email_address="j@p.cx", display_name="jens persson", password="")
    g = Group(group_name="admin", display_name="Admin")
    g.addUser(u)


    plugins.find_plugins("plugins")
    p = Post(title="What I did on My Holidays",
             text="""Lorem ipsum dolor sit amet, consectetuer adipiscing
             elit. In ut ligula. Proin fermentum. Maecenas mauris odio,
             elementum quis, tempor non, congue eget, eros. Curabitur
             pede dolor, molestie vel, scelerisque quis, vehicula sed,
             dui. Donec ac dolor non nisl porta egestas. Pellentesque at
             mi. Pellentesque feugiat, neque eu posuere tincidunt, massa
             dolor semper leo, eu ullamcorper eros nisi vel lacus. Mauris
             purus nulla, euismod at, tempus ac, imperdiet pretium, pede""",
             created = datetime.datetime(2007, 1, 13, 22, 00, 00),
             published = True,
             author="Twoflower", tags="Agatean, Luggage, Ankh-Morpork, Rincewind")
    Comment(title = "No you dindn't",
                text = """Lorem ipsum dolor sit amet, consectetuer adipiscing
                elit. In ut ligula. Proin fermentum. Maecenas mauris odio""",
                author = "Rincewind the Wizzard",
                created = datetime.datetime(2007, 1, 13, 22, 00, 00),
                authorlink = "http://hex.uu.am/~rincewind/",
                post = p,
                status = "published"
                )
    Comment(title = "Oh yes I did",
                text = """Lorem ipsum dolor sit amet, consectetuer adipiscing
                elit. In ut ligula. Proin fermentum. Maecenas mauris odio""",
                author = "Twoflower",
                post = p,
                status = "published"
                )

    p = Post(title="The Truth Shall Make Ye Fret",
             text="""Lorem ipsum dolor sit amet, consectetuer adipiscing
             elit. In ut ligula. Proin fermentum. Maecenas mauris odio,
             elementum quis, tempor non, congue eget, eros. Curabitur
             pede dolor, molestie vel, scelerisque quis, vehicula sed,
             dui. Donec ac dolor non nisl porta egestas. Pellentesque at
             mi. Pellentesque feugiat, neque eu posuere tincidunt, massa
             dolor semper leo, eu ullamcorper eros nisi vel lacus. Mauris
             purus nulla, euismod at, tempus ac, imperdiet pretium, pede""",
             published = False,
             author="William de Worde & Sacharissa Cripslock.",
             tags="Deep Bone, Mr. Pin and Mr. Tulip")
    Comment(title = u"This is -ing good wood!",
                text = """Lorem ipsum dolor sit amet, consectetuer adipiscing
                elit. In ut ligula. Proin fermentum. Maecenas mauris odio""",
                author = "Mr. Tulip",
                post = p
                )

    # Set up some plugins
    PluginConfiguration.selectBy(name=u"Textile")[0].active=True
    PluginConfiguration.selectBy(name=u"Archive")[0].active=True
    hub.commit()

if __name__ == '__main__':
    clear_all()
    load_startdata()
